﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace WP7Guide
{
    using System.Collections.Generic;
    using System.Collections.ObjectModel;

    public class ServiceViewModel : ViewModelBase
    {
        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                if (value != _name)
                {
                    _name = value;
                    NotifyPropertyChanged("Name");
                }
            }
        }

        private string _currentEvent;
        public string CurrentEvent
        {
            get
            {
                return _currentEvent;
            }
            set
            {
                if (value != _currentEvent)
                {
                    _currentEvent = value;
                    NotifyPropertyChanged("CurrentEvent");
                }
            }
        }

        private string _timeLeft;
        private ObservableCollection<EventViewModel> _events;

        /// <summary>
        /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
        /// </summary>
        /// <returns></returns>
        public string TimeLeft
        {
            get
            {
                return _timeLeft;
            }
            set
            {
                if (value != _timeLeft)
                {
                    _timeLeft = value;
                    NotifyPropertyChanged("TimeLeft");
                }
            }

        }


        public ObservableCollection<EventViewModel> Events
        {
            get {
                if (_events == null)
                {
                    _events = new ObservableCollection<EventViewModel>();
                }
                return _events;
            }
            set {
                CleanUpEvents(value);
                _events = value;
                NotifyPropertyChanged("Events");
            }
        }

        private void CleanUpEvents(ObservableCollection<EventViewModel> value)
        {
            if (value != null)
            {
                var now = DateTime.Now;
                var initialCount= value.Count;
                for (int i = 0; i < initialCount; i++)
                {
                    var eventViewModel = value[0];
                    if (eventViewModel.EndTime < now)
                    {
                        value.RemoveAt(0);
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }

        public string RefID { get; set; }
    }
}